package com.block.sms.service.send;

import cn.hutool.core.util.ByteUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.common.util.LocalDateUtil;
import com.block.db.entity.SmsConfigInfo;
import com.block.db.entity.SmsTemplateInfo;
import com.block.sms.dto.*;
import com.block.sms.service.ISmsSendService;
import lombok.Data;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class YiMeiSmsSendServiceImpl implements ISmsSendService {

    private static final Logger log = LogFactory.getCommLog();

    @Override
    public SmsResultDTO sendSms(SmsParamDTO smsParamDTO, SmsTemplateInfo smsTemplate, SmsConfigInfo smsConfigInfo) {

        SmsResultDTO resultDTO = new SmsResultDTO();
        JSONArray paramArr = smsParamDTO.getParamArray();

        String timestamp = LocalDateUtil.getNowDateStr(LocalDateUtil.TOTAL_DATE_TIME_NOT_SPACE_GANG);
        String content = StrUtil.format(smsTemplate.getTemplateContent(), paramArr.stream().toArray());

        String sign = SecureUtil.md5(smsConfigInfo.getAccountNo()+smsConfigInfo.getAccountSecret()+timestamp);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appId", smsConfigInfo.getAccountNo());
        paramMap.put("timestamp", timestamp);
        paramMap.put("sign", sign);
        paramMap.put("mobiles", smsParamDTO.getMobie());
        paramMap.put("content", content);

        String bodyResult = HttpUtil.createPost(smsConfigInfo.getSendUrl())
                .form(paramMap)
                .execute()
                .body();
        log.info("【亿美短信服务】发送结束【requesturl】{}【paramMap】{}【bodyResult】{}",
                smsConfigInfo.getSendUrl(), JSON.toJSONString(paramMap), bodyResult);

        JSONObject resultJson = JSON.parseObject(bodyResult);
        String code = resultJson.getString("code");

        resultDTO.setResult(bodyResult);
        resultDTO.setCode(String.valueOf(code));
//        resultDTO.setMessage(message);
//        resultDTO.setOutOrderNo(String.valueOf(msg_id));
        if (StrUtil.equals("SUCCESS", code)) {

            JSONArray jsonArray = resultJson.getJSONArray("data");
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            String smsId = jsonObject.getString("smsId");
            resultDTO.setOutOrderNo(smsId);

            resultDTO.setSuccess(true);
            return resultDTO;
        }
        resultDTO.setMessage(bodyResult);
        return resultDTO;
    }

    @Override
    public SmsBalanceDTO queryBalanceInfo(SmsConfigInfo smsConfigInfo) {

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("appId", smsConfigInfo.getAccountNo());

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("requestTime", System.currentTimeMillis());
        paramMap.put("requestValidPeriod", 60);

        HttpResponse httpResponse = HttpUtil.createPost(smsConfigInfo.getQueryUrl())
                .headerMap(headerMap, true)
                .body(SecureUtil.aes(smsConfigInfo.getAccountSecret().getBytes()).encrypt(JSON.toJSONString(paramMap)))
                .execute();

        String result = httpResponse.header("result");
        System.out.println(result);

        byte[] bytes = httpResponse.bodyBytes();

        String s = SecureUtil.aes(smsConfigInfo.getAccountSecret().getBytes()).decryptStr(bytes);
        System.out.println(s);

//        log.info("【亿美短信服务】发送结束【requesturl】{}【paramMap】{}【bodyResult】{}",
//                smsConfigInfo.getSendUrl(), JSON.toJSONString(paramMap), bodyResult);


        return null;
    }

    @Override
    public QueryResultDTO querySendResult(QueryParamDTO queryParamDTO, SmsConfigInfo smsConfigInfo) {

        String timestamp = LocalDateUtil.getNowDateStr(LocalDateUtil.TOTAL_DATE_TIME_NOT_SPACE_GANG);

        String sign = SecureUtil.md5(smsConfigInfo.getAccountNo()+smsConfigInfo.getAccountSecret()+timestamp);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appId", smsConfigInfo.getAccountNo());
        paramMap.put("timestamp", timestamp);
        paramMap.put("sign", sign);
        paramMap.put("number", 500);

        String url = "http://www.btom.cn:8080/simpleinter/getReport";
        String bodyResult = HttpUtil.createGet(url)
                .form(paramMap)
                .execute()
                .body();
        log.info("【亿美短信服务】发送结束【requesturl】{}【paramMap】{}【bodyResult】{}",
                smsConfigInfo.getSendUrl(), JSON.toJSONString(paramMap), bodyResult);

        return null;
    }
}
